{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d1b455f-9335-418d-a245-d70433dc0b3d",
   "metadata": {},
   "source": [
    "# Add Front Matter to Text\n",
    "\n",
    "Using Python pkg python-frontmatter to add front matter to content."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf46515e-778d-4554-a8bf-75418b0404fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install python-frontmatter==1.1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76e97318-cf32-4608-b5c8-7f6968d7af8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 show python-frontmatter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a374818f-d65c-4fb5-bf02-a25e60628dbd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import frontmatter\n",
    "\n",
    "# Define the text content\n",
    "text = \"\"\"# Roasted Garlic and Tomato Pasta\n",
    "\n",
    "## Ingredients\n",
    "\n",
    "- 8 oz pasta (linguine, spaghetti, or your preferred type)\n",
    "- 1 head of garlic\n",
    "- 1 pint cherry tomatoes\n",
    "- 1/4 cup olive oil, plus more for drizzling\n",
    "- 1/2 cup fresh basil leaves, chopped\n",
    "- Salt and pepper to taste\n",
    "- Parmesan cheese (optional)\n",
    "\n",
    "## Instructions\n",
    "\n",
    "1. Preheat your oven to 400°F (200°C).\n",
    "2. Cut off the top of the garlic head to expose the cloves. Drizzle with olive oil and wrap in foil. Roast in the oven for 30-40 minutes, until the cloves are soft and golden.\n",
    "3. On a baking sheet, toss the cherry tomatoes with a drizzle of olive oil, salt, and pepper. Roast in the oven for 15-20 minutes, until the tomatoes are blistered and bursting.\n",
    "4. Cook the pasta according to package instructions until al dente. Reserve 1/2 cup of the pasta cooking water before draining.\n",
    "5. In a large bowl, combine the cooked pasta, roasted garlic cloves (squeezed out of their skins), roasted tomatoes, and the reserved pasta cooking water. Toss everything together, adding more olive oil if needed to create a light sauce.\n",
    "6. Stir in the chopped basil and season with salt and pepper to taste.\n",
    "7. Serve hot, garnished with grated Parmesan cheese if desired.\n",
    "\n",
    "Yield: 4 servings\"\"\"\n",
    "\n",
    "# Define the dictionary to be added as frontmatter\n",
    "metadata = {\n",
    "    \"contentType\": \"recipe\",\n",
    "    \"recipeTitle\": \"Roasted Garlic and Tomato Pasta\",\n",
    "    \"keyIngredients\": [\"pasta\", \"garlic\", \"tomatoes\", \"olive oil\", \"basil\"],\n",
    "    \"servings\": 4,\n",
    "    \"tags\": [\"dinner\", \"Italian\", \"vegetarian\"],\n",
    "    \"summary\": \"A simple pasta dish featuring roasted garlic and cherry tomatoes in a light sauce\"\n",
    "}\n",
    "\n",
    "# Create a frontmatter object with the metadata and content\n",
    "post = frontmatter.Post(text, **metadata)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce87bbc6-cba6-4ef9-b946-cf86f6227af3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Text with Front Matter:\")\n",
    "print(frontmatter.dumps(post))\n",
    "print(\"\\n---\\n\")\n",
    "print(\"You can also extract the Front Matter as a dict:\")\n",
    "print(post.metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d1c4771-7f11-4e92-85e6-9a7142d90b99",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
